/*******************************************************************************
 * Copyright (c) 2011 - 2012 TXT e-solutions SpA
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 *
 * Authors:
 *     Cristoforo Seccia (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package it.txt.access.capability.commons.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 *
 * @author Cristoforo Seccia (TXT e-solutions SpA)
 */
public class CalendarUtils {

    public static final String DATE_PRINT_PATTERN = "yyyy-MM-dd 'at' HH:mm:ss.SSS Z";

    public static final int AFTER_THAN = 1;

    public static final int BEFORE_THAN = -1;
    
    public static final int EQUAL_TO = 0;

    public static int comapareDataToAnotherDate(Date date1, Date date2) {

        Calendar givenCalendar1 = Calendar.getInstance();
        givenCalendar1.setTime(date1);

        Calendar givenCalendar2 = Calendar.getInstance();
        givenCalendar2.setTime(date2);

        if (givenCalendar1.after(givenCalendar2)) {
            return AFTER_THAN;
        }
        else if (givenCalendar1.before(givenCalendar2)) {
            return BEFORE_THAN;
        }
        else {
            return EQUAL_TO;
        }
    }

    public static int comapareDataToAnotherDate(XMLGregorianCalendar date1, XMLGregorianCalendar date2) {
        return comapareDataToAnotherDate(date1.toGregorianCalendar().getTime(), date2.toGregorianCalendar().getTime());
    }


    public static int comapareDataToCurrentDate(Date date) {

        Calendar givenCalendar = Calendar.getInstance();
        givenCalendar.setTime(date);
        Calendar currentCalendar = Calendar.getInstance();
        currentCalendar.setTime(new Date());
        
        if (givenCalendar.after(currentCalendar)) {
            return AFTER_THAN;
        }
        else if (givenCalendar.before(currentCalendar)) {
            return BEFORE_THAN;
        }
        else {
            return EQUAL_TO;
        }
    }

    public static int comapareDataToCurrentDate(XMLGregorianCalendar calendar) {
        return comapareDataToCurrentDate(calendar.toGregorianCalendar().getTime());
    }

    public static XMLGregorianCalendar newXmlGregorianCalendarFromDateAndTime(Date dayToSet, Date timeToSet)
            throws DatatypeConfigurationException {

        GregorianCalendar givenDay = new GregorianCalendar();
        givenDay.setTime(dayToSet);
        
        GregorianCalendar givenTime = new GregorianCalendar();
        givenTime.setTime(timeToSet);

        givenDay.set(Calendar.HOUR_OF_DAY, givenTime.get(Calendar.HOUR_OF_DAY));
        givenDay.set(Calendar.MINUTE, givenTime.get(Calendar.MINUTE));
        givenDay.set(Calendar.SECOND, givenTime.get(Calendar.SECOND));

        DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
        XMLGregorianCalendar result = dataTypeFactory.newXMLGregorianCalendar(givenDay);

        return result;
    }
}
